<template>
  <div class="register">
    <h2>{{ $t('message.register') }}</h2>
    <form @submit.prevent="register">
      <div class="form-group">
        <label for="username">{{ $t('message.username') }}</label>
        <input 
          type="text" 
          id="username" 
          v-model="username" 
          required 
          :placeholder="$t('message.pleaseEnterUsername')"
        />
      </div>
      <div class="form-group">
        <label for="email">邮箱</label>
        <input 
          type="email" 
          id="email" 
          v-model="email" 
          required 
          :placeholder="$t('message.pleaseEnterEmail')"
        />
      </div>
      <div class="form-group">
        <label for="password">{{ $t('message.password') }}</label>
        <input 
          type="password" 
          id="password" 
          v-model="password" 
          required 
          :placeholder="$t('message.pleaseEnterPassword')"
        />
      </div>
      <div class="form-group">
        <label for="role">{{ $t('message.role') }}</label>
        <select id="role" v-model="role" required>
          <option value="PARENT">{{ $t('message.parent') }}</option>
          <option value="TEACHER">{{ $t('message.teacher') }}</option>
          <option value="ASSISTANT">{{ $t('message.assistant') }}</option>
          <option value="ADMIN">{{ $t('message.admin') }}</option>
        </select>
      </div>
      <button type="submit" :disabled="loading">
        {{ loading ? $t('message.registering') : $t('message.register') }}
      </button>
    </form>
    <p>{{ $t('message.alreadyHaveAccount') }}<router-link to="/login">{{ $t('message.loginHere') }}</router-link></p>
  </div>
</template>

<script>
import { useAuthStore } from '@/stores/auth'
import { useRouter } from 'vue-router'

export default {
  name: 'RegisterView',
  setup() {
    const authStore = useAuthStore()
    const router = useRouter()
    
    return {
      authStore,
      router
    }
  },
  data() {
    return {
      username: '',
      email: '',
      password: '',
      role: 'PARENT',
      loading: false
    }
  },
  methods: {
    async register() {
      this.loading = true
      try {
        await this.authStore.register({
          username: this.username,
          email: this.email,
          password: this.password,
          role: this.role
        })
        await this.router.push('/login')
      } catch (error) {
        alert(this.$t('message.registrationFailed') + ': ' + error.message)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.register {
  max-width: 400px;
  margin: 2rem auto;
  padding: 2rem;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.form-group {
  margin-bottom: 1rem;
  text-align: left;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
}

.form-group input, .form-group select {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

button {
  width: 100%;
  padding: 0.75rem;
  background-color: #2ecc71;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:disabled {
  background-color: #bdc3c7;
  cursor: not-allowed;
}
</style>